package com.itheima.task;

import com.itheima.common.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Slf4j
@Component
public class AutoDeleteFileTasK {

    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0 */10 * * * *")
    public void processDeleteImage(){
        SetOperations setOperations = redisTemplate.opsForSet();
        Set uploadSetmeal = setOperations.members("uploadSetmeal");//上传的图片
        Set setmealPath = setOperations.members("setmealPath"); //完成套餐的的图片
        uploadSetmeal.removeAll(setmealPath); // 计算差集，保留只存在于uploadSetmeal中的元素
        log.info("正在清除垃圾图片");
        for (Object filePath : uploadSetmeal) {
            aliOssUtil.delete(filePath.toString());
        }

    }
}
